<!--  -->
<template>
    <div>
        <!-- 面包屑存放 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
                        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addClick">添加用户</el-button>
                </el-col>
            </el-row>
            <el-table :data="userList" stripe border>
                <el-table-column type="index" label="#">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>

                <el-table-column prop="mg_state" label="状态">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" @change="handleUsersState(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editClick(scope.row)">
                        </el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteClick(scope.row.id)">
                        </el-button>
                        <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
                            <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
                        </el-tooltip>

                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10,50,100]" :page-size="queryInfo.pagesize"
                layout="  sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>

        <edit-form :addForm="addForm" :addDialogVisible="addDialogVisible" :dialogTitle="dialogTitle"
            @addUsers="addUsers" @editUsers="editUsers" @cancel="cancel"></edit-form>

    </div>
</template>

<script>
    import { getUsers, putUsers, postUsers, putUsersId } from '@/api/users'
    import EditForm from './modules/editForm.vue'
    export default {
        name: 'users',
        components: { EditForm },
        data() { 
            return {
                queryInfo: {
                    query: '',
                    pagenum: 1,
                    pagesize: 5
                },
                userList: [],
                total: 0,
                addDialogVisible: false,
                addForm: {
                    username: '',
                    password: '',
                    email: '',
                    mobile: '',

                },

                dialogTitle: '添加'
            }
        },
        created() {
            this.getUserList();
        },
        methods: {
            async getUserList() {
                // const { data: res } = await this.$http.get('users', { params: this.queryInfo })
                getUsers(this.queryInfo).then(res => {
                    if (res.meta.status !== 200) {
                        return this.$message.error(res.meta.msg)
                    }
                    this.userList = res.data.users
                    this.total = res.data.total
                })


            },
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize
                this.getUserList()
            },
            handleCurrentChange(newNum) {
                this.queryInfo.pagenum = newNum
                this.getUserList()
            },
            handleUsersState(userinfo) {
                // const { data: res } = await this.$http.put(`users/${userinfo.id}/state/${userinfo.mg_state}`)
                putUsers(userinfo).then(res => {
                    if (res.meta.status !== 200) {
                        userinfo.mg_state = !userinfo.mg_state
                        return this.$message.error('更新用户状态失败')
                    }
                    return this.$message.success('更新用户状态成功')
                })

            },
            addClick() {
                this.addDialogVisible = true
                this.dialogTitle = '添加'
            },
            addUsers(addForm) {
                // const { data: res } = await this.$http.post('users', this.addForm)
                postUsers(addForm).then(res => {
                    if (res.meta.status !== 201) {
                        return this.$message.error(res.meta.msg)
                    } else {
                        this.$message.success(res.meta.msg)
                        this.addDialogVisible = false
                        this.getUserList();
                    }
                })

            },
            // 编辑
            editClick(record) {
                this.addDialogVisible = true
                this.dialogTitle = '编辑'
                this.addForm = JSON.parse(JSON.stringify(record))
            },
            // 编辑的确定
            editUsers(addForm) {
                putUsersId(addForm).then(res => {
                    if (res.meta.status !== 200) {
                        return this.$message.error(res.meta.msg)
                    } else {
                        this.$message.success(res.meta.msg)
                        this.addDialogVisible = false
                        this.getUserList();
                    }
                })
            },
            cancel() {
                this.addDialogVisible = false
            },
            async deleteClick(userId) {
                const resultConfim = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err)
                if (resultConfim !== 'confirm') {
                    return this.$message.info('用户取消了删除')
                }
                const { data: res } = await this.$http.delete('users/' + userId)


                if (res.meta.status !== 200) {
                    return this.$message.error('删除用户失败')
                }
                this.$message.success('删除用户成功')

                this.getUserList();


            }
        }
    }
</script>
<style lang="scss" scoped>
    .el-breadcrumb {
        margin-bottom: 15px;
        font-size: 12px;
    }
</style>